//
//  KHHTravelNotesDetailLocationPageViewController.m
//  Travel
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHTravelNotesDetailLocationPageViewController.h"
#import "KHHTravelNotesDetailLocationPageTableViewCell.h"
#import "KHHTravelNotesDetailPageViewController.h"   ///////两页头文件互相引用了
#import "UIImage+GIF.h"
#import "UIImageView+WebCache.h"
#import "StarView.h"

@interface KHHTravelNotesDetailLocationPageViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)KHHRecommendModel *modelAllData; //保存最外层对象
@property (nonatomic, retain)NSMutableArray *modelTripArr; //保存所有trip对象
@property (nonatomic, retain)NSMutableArray *modelUserArr; //保存所有用户对象
@property (nonatomic, retain)NSMutableArray *modelWaypointsArr; //保存所有图片对象
@property (nonatomic, retain)NSMutableArray *modelTripsArr; //保存trips对象 获取文章副标题
@property (nonatomic, retain)NSMutableArray *modelHottest_places; //保存背景图片对象
@property (nonatomic, retain)UIImageView *backImg; //背景图集

@end

@implementation KHHTravelNotesDetailLocationPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //[self addScreenEdgePan];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"leftBack"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    
    [self getData];
    
    //菊花
    [CXLoadMBProgress loadMBProgress:self.view];
}

#pragma mark - 获取data
- (void)getData
{
    NSString *url = nil;
    if (self.type != nil && self.valueId!= nil) {
        //另一个tabbar传来的
        url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@", self.type, self.valueId];
        
    } else {
        //我传过来的
        url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@", self.waypointsModel.type, self.waypointsModel.poiID];
    }
    
    [CXNetHandle getUrl:url cookie:nil result:^(id result) {
        
        //移除菊花
        [CXLoadMBProgress removeMBProgress:self.view];
        
        NSMutableDictionary *tempdic = result;
        self.modelAllData = [[KHHRecommendModel alloc]init];
        [self.modelAllData setValuesForKeysWithDictionary:tempdic];
        
        NSMutableArray *tripArr = [NSMutableArray array];
        NSMutableArray *userArr = [NSMutableArray array];
        NSMutableArray *waypointsArr = [NSMutableArray array];
        
        self.modelTripsArr = [NSMutableArray array];
        self.modelHottest_places = [NSMutableArray array];
        
        self.modelTripsArr  = [KHHRecommendModel transformWithArray:tempdic[@"impressions"][@"trips"]];
        self.modelHottest_places = [KHHRecommendModel transformWithArray:tempdic[@"hottest_places"]];
        
        for (NSMutableDictionary *dic in  tempdic[@"impressions"][@"trips"]) {
            
            [tripArr addObject:dic[@"trip"]]; //所有印象中trip在一个数组中
            [userArr addObject:dic[@"trip"][@"user"]]; //所有用户信息在一个数组中
            [waypointsArr addObject:dic[@"waypoints"]]; //所有waypoints数组在一个数组中
        }
        
        self.modelWaypointsArr = [NSMutableArray array];
        self.modelTripArr = [NSMutableArray array];
        self.modelUserArr = [NSMutableArray array];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSMutableArray *arr in waypointsArr) {
            tempArr = [KHHRecommendModel transformWithArray:arr]; //保存所有图片对象
            [self.modelWaypointsArr addObject:tempArr];
        }
        
        self.modelTripArr = [KHHRecommendModel transformWithArray:tripArr]; //保存所有trip对象
        self.modelUserArr = [KHHRecommendModel transformWithArray:userArr]; //保存所有user对象
        
        [self createTableView];
        
    }];
}

#pragma mark - 创建tableView
- (void)createTableView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(264 , 0, 0, 0);
    // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    //注册cell
    [self.tableView registerClass:[KHHTravelNotesDetailLocationPageTableViewCell class] forCellReuseIdentifier:@"reUse"];
    
    //设置tableView的contentView距离上边界264
    self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, -264, WIDTH, 264)];
    if (self.modelHottest_places.count != 0) {
            [self.backImg sd_setImageWithURL:[NSURL URLWithString:[self.modelHottest_places[0] photo]] placeholderImage:[UIImage imageNamed:@"city"]];
    }

    self.backImg.contentMode = UIViewContentModeScaleAspectFill; //设置img高度改变时 宽度也改变
    self.backImg.clipsToBounds = YES;
    [self.tableView addSubview:self.backImg];
    
    
    //创建返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(13, 25, 30, 30);
    [self.view addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"leftBack"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backToLast) forControlEvents:UIControlEventTouchUpInside];
    
   // [[self class]BoolIsNight:self.tableView];
    
}

#pragma mark - back 按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 背景图下滑改变宽高
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    //  NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 170 ){
        CGRect frame = self.backImg.frame;
        frame.origin.y = y; //image 的frame不断往上偏移
        frame.size.height = -y; //tableView向下偏移了多少 高度就增加多少
        self.backImg.frame = frame;
        
    }
    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -64) {
        self.navigationController.navigationBar.alpha = (64 + scrollView.contentOffset.y) / 64 - 0.3;
    }
    else if (scrollView.contentOffset.y > 0) {
        self.navigationController.navigationBar.alpha = 1.0;
    }
    else {
        self.navigationController.navigationBar.alpha = 0.0;
        
    }
    
}

#pragma mark - 视图要消失时候 设置bar出现
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.alpha = 1.0;
}

#pragma mark - tableView cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelTripArr.count;
}

#pragma mark - tableView 头部分区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 190;
}
#pragma mark - 分区头view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240)];
    view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
    
    
    UIImageView *imgIcon = [[UIImageView alloc]init];
    imgIcon.frame = CGRectMake(WIDTH / 2 - WIDTH /10 ,- WIDTH / 10 , WIDTH / 5, WIDTH/5);
    imgIcon.layer.masksToBounds = YES;
    imgIcon.layer.cornerRadius = WIDTH / 10;
    
    
    //标题
    UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, WIDTH - 40, 30)];
    labelTitle.textAlignment = NSTextAlignmentCenter;
    labelTitle.text = self.modelAllData.name;
    labelTitle.font = [UIFont systemFontOfSize:24];
    
    [view addSubview:labelTitle];
    
    
    //评分
    StarView *star = [[StarView alloc] initWithFrame:CGRectMake((WIDTH - 83)/2 , labelTitle.frame.origin.y + labelTitle.frame.size.height + 20 , 15 * 5 + 8, 15) rating:self.modelAllData.rating.integerValue];
    [view addSubview:star];
    
    
    /* 横线 点 标题*/
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(20 ,labelTitle.frame.origin.y + labelTitle.frame.size.height + 45 + 30, (WIDTH - 100 - 30 - 30 - 40) / 2, 1);
    label.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
    [view addSubview:label];
    
    
    UILabel *labelPoint = [[UILabel alloc]init];
    labelPoint.frame = CGRectMake(label.frame.origin.x + label.frame.size.width + 10, label.frame.origin.y - 2, 5, 5);
    labelPoint.layer.masksToBounds = YES;
    labelPoint.layer.cornerRadius = 2.5;
    labelPoint.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
    [view addSubview:labelPoint];
    
    
    UILabel *labelImpression = [[UILabel alloc]init];
    labelImpression.frame = CGRectMake(labelPoint.frame.origin.x + 15, labelPoint.frame.origin.y - 10, 100, 30);
    labelImpression.textAlignment = NSTextAlignmentCenter;
    labelImpression.text = @"行走印象";
    labelImpression.textColor = [UIColor darkTextColor];
    labelImpression.center = CGPointMake(labelTitle.center.x, labelPoint.frame.origin.y - 3 );
    labelImpression.font = [UIFont systemFontOfSize:22];
    labelImpression.textColor = [UIColor darkTextColor];
    [view addSubview:labelImpression];
    
    UILabel *labelPoint1 = [[UILabel alloc]init];
    labelPoint1.frame = CGRectMake(labelImpression.frame.origin.x + labelImpression.frame.size.width + 10, labelPoint.frame.origin.y , 5, 5);
    labelPoint1.layer.masksToBounds = YES;
    labelPoint1.layer.cornerRadius = 2.5;
    labelPoint1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
    [view addSubview:labelPoint1];
   
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.frame = CGRectMake(labelPoint1.frame.origin.x + 2 + 15, labelPoint1.frame.origin.y, label.frame.size.width, 1);
    label1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
    [view addSubview:label1];
    
    
    
    //夜间模式
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
        view.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
        labelTitle.textColor = [UIColor whiteColor];
        labelImpression.textColor = [UIColor whiteColor];
        
    } else {
        
        view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    }
    return  view;
    
    
    
}
#pragma mark - tableView cell数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KHHTravelNotesDetailLocationPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reUse"];
    
    //取消cell点击方法
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.modelTrip = self.modelTripArr[indexPath.row];
    cell.modelAllDate = self.modelAllData;
    cell.waypointsArr = self.modelWaypointsArr[indexPath.row];
    cell.modelUser = self.modelUserArr[indexPath.row];
    cell.modelTrips = self.modelTripsArr[indexPath.row];
    return cell;
}

#pragma mark - tableView 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KHHRecommendModel *tempModel = self.modelTripsArr[indexPath.row];
    CGFloat hSubtitle = [[self class]heightWithText:tempModel.trip_text fontSize:14 Width:CWIDTH - 20];
    
    //判断副标题是否有内容
    if (tempModel.trip_text.length != 0) {
        
        return  10 +  CWIDTH / 9 + 15 + hSubtitle +  10 +  150 + 20 ;
        
    } else {
        
        return  10 +  CWIDTH / 9  +  10 +  150 + 20 ;
    }
}

#pragma mark - tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    KHHTravelNotesDetailPageViewController *detailPage = [[KHHTravelNotesDetailPageViewController alloc] init];
    
    detailPage.locationImpressionToDetailID = ((KHHRecommendModel *)self.modelTripArr[indexPath.row]).notesId;
    detailPage.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailPage animated:YES];
    
    
}
#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.tableView addGestureRecognizer:screenEdgePan];
    
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
